#!/bin/sh
export LANG="en_US.UTF-8"
_JAVA=$JAVA_HOME/bin/java
_APP=$(cd $(dirname $0); pwd)
_JARS=.:$_APP/config
 #-Xdebug -Xrunjdwp:transport=dt_socket,address=6062,suspend=n,server=y
_PARAMS="-server \
 -Dfile.encoding=UTF-8 \
 -Djava.net.preferIPv4Stack=true \
 -Xms256m -Xmx2g -Xmn640M  \
 -XX:PermSize=384M -XX:MaxPermSize=384M \
 -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90 \
 -XX:+DisableExplicitGC \
 -XX:+UseParNewGC \
 -XX:StackShadowPages=20 \
 -XX:ParallelCMSThreads=2 -XX:ParallelGCThreads=3 \
 -XX:+UseTLAB -XX:+AggressiveOpts -XX:+OptimizeStringConcat \
 -XX:+UseStringCache -XX:+UseCompressedOops \
 -XX:+UseConcMarkSweepGC \
 -XX:+UseCMSCompactAtFullCollection \
 -XX:CMSFullGCsBeforeCompaction=3 \
 -XX:+CMSClassUnloadingEnabled \
 -XX:-CMSParallelRemarkEnabled \
 -XX:CMSMaxAbortablePrecleanTime=5 \
 -XX:CMSInitiatingOccupancyFraction=70 \
 -XX:SoftRefLRUPolicyMSPerMB=0 \
 -XX:+HeapDumpOnOutOfMemoryError \
 -Djava.security.egd=file:/dev/./urandom \
 -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.EPollSelectorProvider"
_LOG="/dev/null"
start() {
	[ -d $_APP ] || exit 5;
	echo "Server is starting..."
	cd $_APP
	nohup $_JAVA $_PARAMS -jar $_APP/hzzbs*.jar > $_LOG 2>&1 &
	echo "Server is started..."
}
stop() {
	_kill=15
    _timeout=30
    if [[ "" -ne "$1" ]]; then
        _kill=$1
    fi
    if [[ "" -ne "$2" ]]; then
        _timeout=$2
    fi
    echo 'kill -'$_kill"..find Server process.";
    i=0
    while test $i -lt $_timeout; do
        pid=$(ps -ef | grep $_APP | grep -v grep | grep -v appserver | awk '{print $2}')
        if [ ! -n "$pid" ]; then
            if [ $i -eq 0 ]; then
                echo "Server process is not find."
            fi
            if [ $i -gt 0 ]; then
                echo 'kill -'$_kill'..stop...success.'
            fi
            return 1;
        else
            if [ $i -eq 0 ]; then
                echo  'kill -'$_kill"..Waitting server stop."
            fi
        fi
        echo  'kill -'$_kill'..stop...'$i'..'
        if [ $i -eq 0 ]; then
            for id in $pid
            do
                kill -$_kill $id;
            done
        fi
        i=$(expr $i + 1)
        sleep 1
    done;
    return 0;
}
restart() {
	if stop 15 30; then
		stop 9 30;
	fi
	start
}
state() {
	pid=$(ps -ef | grep $_APP | grep -v grep | grep -v appserver | awk '{print $2}')
	if [ ! -n "$pid" ]; then
		echo "Server process is not find."
	else
	    for id in $pid
        do
            echo "Server is run pid="$id;
        done
	fi
}
case "$1" in
	start)
		$1
		;;
	stop)
		$1
		;;
	restart)
		$1
		;;
	state)
		$1
		;;
   *)
    echo $"Useage: $0 {start|stop|state|restart}"
    exit 2
esac